<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="收支数目" prop="payment">
      <el-input v-model.number="dataForm.payment" :point="2" placeholder="收支数目"></el-input>
    </el-form-item>
    <el-form-item label="收支类型" prop="type">
      <el-radio-group v-model="dataForm.type">
        <el-radio label="0">其他类型</el-radio>
        <el-radio label="1">食物类</el-radio>
        <el-radio label="2">住房类</el-radio>
        <el-radio label="3">生活类</el-radio>
        <el-radio label="4">日化类</el-radio>
        <el-radio label="5">通讯类</el-radio>
        <el-radio label="6">交通类</el-radio>
        <el-radio label="7">工资</el-radio>
        <el-radio label="8">转账</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="收支时间" prop="paydata">
      <el-col>
      <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.paydata" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
    </el-col>
    </el-form-item>
    <el-form-item label="原因" prop="reason">
      <el-input v-model="dataForm.reason" placeholder="原因"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :loading='loading' @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        loading: false,
        visible: false,
        dataForm: {
          id: 0,
          payment: '',
          type: '',
          paydata: '',
          reason: '',
          remark: '',
          delFlag: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: ''
        },
        dataRule: {
          payment: [
            { required: true, message: '收支数目不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '请选择收支类型', trigger: 'change' }
          ],
          paydata: [
            { required: true, message: '收支时间不能为空', trigger: 'blur' }
          ],
          reason: [
            { required: true, message: '原因不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/pocket/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.payment = data.pocket.payment
                this.dataForm.paydata = data.pocket.paydata
                this.dataForm.reason = data.pocket.reason
                this.dataForm.remark = data.pocket.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.loading = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/pocket/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'payment': this.dataForm.payment,
                'paydata': this.dataForm.paydata,
                'reason': this.dataForm.reason,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
